package com.endava.controllers;

import java.util.List;
import java.util.logging.Logger;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.endava.model.Employee;
import com.endava.model.Login;
import com.endava.service.EmployeeService;
import com.endava.service.LoginService;

/**
 * Controller used to manage the login process.
 * 
 * @author acojocar
 * 
 */
@Controller
public class LoginController {
	
	private final static Logger LOGGER = Logger.getLogger("Logger for LoginController");

	@RequestMapping(value = "/")
	public String home() {
		LOGGER.info("HomeController: Passing through...");
		return EmployeeService.LOGIN_PAGE;
	}

	@SuppressWarnings("resource")
	@RequestMapping(value = "/login.do", method = RequestMethod.POST)
	public String getCredentials(@RequestParam(value = "username") String username, @RequestParam(value = "password") String password,
			ModelMap model) {
		BeanFactory beanFactory = new ClassPathXmlApplicationContext(EmployeeService.APP_CONTEXT);
		EmployeeService employeeService = (EmployeeService) beanFactory.getBean(EmployeeService.EMP_SERVICE_BEAN);
		LoginService loginService = (LoginService) beanFactory.getBean(EmployeeService.LOG_SERVICE_BEAN);
		Login login = loginService.getUserByCredentials(username, password);

		if (login != null) {
			if ((login.getUsername().compareTo(username) == 0) && (login.getPassword().compareTo(password) == 0)) {

				if (login.getUserRole().compareTo(LoginService.USER_ROLE) == 0) {
					Employee user = employeeService.getUserData(login.getUsername());
					model.put("user", user);
					model.put("username", login.getUsername());

					return EmployeeService.USER_PAGE;

				} else if (login.getUserRole().compareTo(LoginService.ADMIN_ROLE) == 0) {

					List<Employee> employees = employeeService.getAllEmployees();
					model.put("employees", employees);

					return EmployeeService.ADMIN_PAGE;
				}
			}

		} else {
			return EmployeeService.FAIL_PAGE;
		}

		return null;
	}

	@SuppressWarnings("resource")
	@RequestMapping(value = "/edit.do", method = RequestMethod.GET)
	public String editUserData(@RequestParam int id, ModelMap model) {
		BeanFactory beanFactory = new ClassPathXmlApplicationContext("appContext.xml");
		EmployeeService employeeService = (EmployeeService) beanFactory.getBean("empService");
		Employee employee = employeeService.getEmployeeById(id);

		if (employee.getId() != 0 && id == employee.getId()) {
			model.addAttribute("id", employee.getId());
			model.addAttribute("firstName", employee.getFirstName());
			model.addAttribute("lastName", employee.getLastName());

			return EmployeeService.EDIT_PAGE;
		} else {
			return EmployeeService.ERROR_PAGE;
		}
	}

	@SuppressWarnings("resource")
	@RequestMapping(value = "/edit_employee.do", method = RequestMethod.POST)
	public String submitEditedEmployee(@RequestParam int id, @RequestParam String department, @RequestParam double salary, ModelMap model) {
		BeanFactory beanFactory = new ClassPathXmlApplicationContext("appContext.xml");
		EmployeeService employeeService = (EmployeeService) beanFactory.getBean("empService");
		Employee employee = employeeService.getEmployeeById(id);
		
		if (employee.getId() != 0 && id == employee.getId()) {
			model.addAttribute("department", department);
			model.addAttribute("salary", salary);
			employeeService.updateEmployee(id, department, salary);

			LOGGER.info("Test from req:  " + department + ", " + salary);
			
			List<Employee> employees = employeeService.getAllEmployees();
			model.put("employees", employees);

			return EmployeeService.ADMIN_PAGE;
		} else {
			return EmployeeService.ERROR_PAGE;
		}
	}

	@SuppressWarnings("resource")
	@RequestMapping(value = "/delete.do", method = RequestMethod.GET)
	public String deleteUser(@RequestParam int id, ModelMap model) {
		BeanFactory beanFactory = new ClassPathXmlApplicationContext("appContext.xml");
		EmployeeService employeeService = (EmployeeService) beanFactory.getBean("empService");
		Employee employee = employeeService.getEmployeeById(id);

		if (employee.getId() != 0 && id == employee.getId()) {
			model.addAttribute("id", employee.getId());
			employeeService.deleteEmployeeById(id);
			List<Employee> employees = employeeService.getAllEmployees();
			model.put("employees", employees);

			return EmployeeService.ADMIN_PAGE;
		} else {
			String errorMsg = "The used with id = " + id + ", does not exist!";
			model.addAttribute("errMsg", errorMsg);
			LOGGER.info(errorMsg);

			return EmployeeService.ERROR_PAGE;
		}

	}

}
